package com.telecomyt.jwportal.activity.channel.adapter;

import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.entity.FixedChannel;

import java.util.List;

/**
 * @author created by chengDa
 * @time 2020/9/27  9:58
 * @description
 */
public class FixedChannelAdapter extends BaseMultiItemQuickAdapter<FixedChannel, BaseViewHolder> {
    private final static int TYPE_NORMAL = 0;
    private final static int TYPE_SEE_MORE = 1;
    private final static int TYPE_HIDE = 2;
    private boolean mOpen = false;//是否是展开状态

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public FixedChannelAdapter(List<FixedChannel> data) {
        super(data);
        addItemType(TYPE_NORMAL, R.layout.activity_fixed_channel_item);
        addItemType(TYPE_SEE_MORE, R.layout.activity_fixed_channel_item2);
        addItemType(TYPE_HIDE, R.layout.activity_fixed_channel_item2);

    }
    @Override
    protected void convert(BaseViewHolder helper, FixedChannel item) {
        RelativeLayout rl_item = helper.getView(R.id.rl_item);

        switch (helper.getItemViewType()) {
            case TYPE_NORMAL: //正常布局
                TextView itemView = helper.getView(R.id.itemView);
                itemView.setText(item.getTitle());
                break;
            case TYPE_HIDE: //收起
                TextView mRTextView = helper.getView(R.id.mRTextView);
                mRTextView.setText("收起");
                rl_item.setOnClickListener(v -> {
                    mOpen = false;
                    notifyDataSetChanged();
                });
                break;
            case TYPE_SEE_MORE://展开
                TextView mRTextView1 = helper.getView(R.id.mRTextView);
                mRTextView1.setText("展开");
                rl_item.setOnClickListener(v -> {
                    mOpen = true;
                    notifyDataSetChanged();
                });
                break;

        }

    }


    @Override
    public int getItemViewType(int position) {
        if (mData.size() <= 12) {
            return TYPE_NORMAL;
        }
        if (mOpen) {
            if (position == mData.size()) {
                return TYPE_HIDE;
            } else {
                return TYPE_NORMAL;
            }
        } else {
            if (position == 11) {
                return TYPE_SEE_MORE;
            } else {
                return TYPE_NORMAL;
            }
        }
    }


    @Override
    public int getItemCount() {
        if (mData == null || mData.size() == 0) {
            return 0;
        }
        if (mData.size() > 12) {
            //若现在是展开状态 条目数量需要+1 "收起"条目
            if (mOpen) {
                return mData.size() + 1;
            } else {
                return 12;
            }
        } else {
            return mData.size();
        }
    }
}
